//-----------------Sonic CD Title Card Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias Object.Value0			:	Object.BarPos
private alias Object.Value1			:	Object.TextPos
private alias Object.Value2			:	Object.TextSize
private alias Object.Value3			:	Object.Timer
private alias Object.Value4			:	Object.FadeR
private alias Object.Value5			:	Object.FadeG
private alias Object.Value6			:	Object.FadeB
private alias Object.Value7			:	Object.CharPos

// HUD Alias
private alias Object.Value1			:	HUD.TimeSeconds
private alias Object.Value2			:	HUD.TimeFrames


// States
private alias 0	:	TITLECARD_FADEIN
private alias 1	:	TITLECARD_SLIDEIN
private alias 2	:	TITLECARD_DISPLAY
private alias 3	:	TITLECARD_SLIDEOUT

// Languages
private alias 0	:	LANG_ENGLISH
private alias 1	:	LANG_FRENCH
private alias 2	:	LANG_ITALIAN
private alias 3	:	LANG_DEUTSCH
private alias 4	:	LANG_SPANISH
private alias 5	:	LANG_JAPANESE

// ControlMode Aliases
private alias -1	:	CONTROLMODE_NONE
private alias  0	:	CONTROLMODE_NORMAL

// Game Mode Aliases
private alias 2	:	MODE_TIMEATTACK

// Priority
private alias 1	:	PRIORITY_ACTIVE

public alias saveRAM[6904] : saved.Sensitivity



event ObjectDraw

	// Set the starting time values
	// (Rather than disabling the HUD timer while the title card is active,
	//  its values are simply reset here every frame until no longer needed)
	HUD[4].TimeSeconds = 100
	HUD[4].TimeFrames  = 0
	
	switch Object.State
	case TITLECARD_FADEIN
		if Object.Timer > 0
			if Object.Timer == 256
				// Start the stage music!
				PlayMusic(0)
				music.volume = 25
				object.scale = 0
			end if
			
			
		//	SetClassicFade(0, 0, 0, Object.Timer)
			Object.Timer -= 8
		else
			// Make the Title Card start appearing
			
			Object.State = TITLECARD_SLIDEIN
			Object.Timer = 0
		end if

		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
		
		object[2].controlMode = CONTROLMODE_NONE
		
		break
		
	case TITLECARD_SLIDEIN
		temp0  = 144
		temp0 += Object.CharPos
				

		if Object.TextPos > temp0
			// Slide the text to the left
			Object.TextPos -= 8
			
			// Now, this part got changed in an update
			
		else
			// -> Updated version - both the text AND the bar have to be on screen for the title card to progress

			if Object.BarPos == 0
				Object.State = TITLECARD_DISPLAY
			end if
		end if

		
		
		if Object.BarPos < 0
			// Move the Bar down
			Object.BarPos += 8
			
			// There used to be code here in older versions, but not anymore...
			// -> Initial version - only the bar needed to be in the proper position for the title card to continue
			//    This had text get incorrectly often
			
		end if
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
		break
		
	case TITLECARD_DISPLAY
		// Hold for a moment to let the Player see what a nice Title Card this is
		if Object.Timer == 160
			// Time's up, start sliding out!

			SetClassicFade(0, 0, 0, Object.Timer)
			Object.Timer -= 8

			Object.State = TITLECARD_SLIDEOUT
		else
			Object.Timer++
		end if
		DrawRect(0, 0, screen.xsize, screen.ysize, 0x00, 0x00, 0x00, 255)
		break
		
	case TITLECARD_SLIDEOUT

		SetClassicFade(0, 0, 0, Object.Timer)
		Object.Timer -= 8
		
		if Object.TextPos < 408
		
			// Move the text to the right, and...
			Object.TextPos += 16
			
			// ...move the bar upwards as well
			Object.BarPos -= 16
			
			
		else

			object[2].controlMode = CONTROLMODE_NORMAL

			// The Title Card's job is done, despawn it
			ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)
			
			// Free Sonic's control, for real this time
		
			
			if Options.GameMode == MODE_TIMEATTACK
				// If in time attack, start proper time progression
				Stage.TimeEnabled = true
			end if
		end if
		break
		
	end switch
	
	// Before drawing, make sure this object hasn't been blanked already
	// (Under certain circumstances, this can otherwise cause major graphical issues!!)
	if Object.Type > TypeName[Blank Object]
		temp0  = 105
		temp0 += Object.CharPos
		
		if lampPostID == 0
			// Draw the Title Card Bar
			DrawSpriteScreenXY(0, screen.xcenter, Object.BarPos)
		end if

	end if
	
end event


event ObjectStartup
	if saved.Sensitivity == 0
		saved.Sensitivity = 6
	end if	

	Delta_HP = 90
	Delta_YourTurn = 1
	Delta_SlenderHP = 100
	Delta_SlenderMercy = 0
	Delta_BecomeGreen = 0
	Delta_ShowMercy = 0
	Delta_Defending = 0
	Delta_Attacker = 0
	Delta_AttackHardMode = 0
	PlayerDied = 0
	SlenderSpared = 0
	Delta_Activate = 0
	
	// Load different sheets based on the Engine's current language

		// LANG_ENGLISH and LANG_JAPANESE share the same sheet
	LoadSpriteSheet("3D_Test/Objects.gif")

	
	// Place the Title Card object into the level and setup its values
	Object[20].Type  = TypeName[TitleCard]
	Object[20].Timer = 384
	
	// Make the Title Card always active, and have it draw above everything
	Object[20].Priority  = PRIORITY_ACTIVE
	Object[20].DrawOrder = 6
	
	// Set initial Bar & Text Positions
	Object[20].BarPos  = -216
	Object[20].TextPos = 336
	

	Object[20].CharPos  = Screen.xcenter
	Object[20].CharPos -= 160
	
	if Object[20].CharPos > 3
		Object[20].CharPos -= 4
	end if
	
	Object[20].CharPos &= 248
	
	Object[20].TextPos += Object[20].CharPos
	Object[20].TextPos += Object[20].CharPos
	
	// "SPECIAL STAGE" is 12 letters
	Object[20].TextSize = 12
	
	// Bump the value by 8 to correspond with the order of the Sprite Frames
	Object[20].TextSize += 8
	
	// Preserve the colour used from the last fade. fadeColor is stored as RRGGBB, in hex
	
	Object[20].FadeR   = fadeColor
	Object[20].FadeR >>= 16
	
	Object[20].FadeG   = fadeColor
	Object[20].FadeG  &= 0x00FF00
	Object[20].FadeG >>= 8
	
	Object[20].FadeB = fadeColor
	Object[20].FadeB &= 0x0000FF
	
	// 0 - Red Bar Frame
	SpriteFrame(-64, 110, 129, 11, 1, 389)
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	Object.TextSize = 12
	Object.TextSize += 8

	temp0 = 136
	temp0 += Object.CharPos
	temp0 <<= 16

	temp1 = 0x500000
	temp1 += Object.YPos
	
	DrawSpriteXY(0, Object.XPos, Object.YPos)
	DrawSpriteXY(5, Object.XPos, temp1)
	DrawSpriteXY(6, Object.XPos, temp1)
	DrawSpriteXY(7, Object.XPos, temp1)
	
	temp0 = 8
	while temp0 < Object.TextSize
		DrawSpriteXY(temp0, Object.XPos, temp1)
		temp0++
	loop
end event


event RSDKLoad
	LoadSpriteSheet("Special/ScoreScreen.gif")
	// 0 - Red Bar Frame
	SpriteFrame(0, 0, 32, 184, 224, 37)
	
	// 1-3 - Act Bubbles, not used here.
	SpriteFrame(96, 96, 48, 48, 41, 1)
	SpriteFrame(96, 96, 48, 48, 90, 1)
	SpriteFrame(96, 96, 48, 48, 139, 1)
	
	// 4 - "Zone" text, not used here.
	SpriteFrame(64, 97, 48, 16, 41, 67)
	
	// 5 - Main part of the "SONIC THE HEGDEHOG CD" bar
	SpriteFrame(72, 81, 160, 16, 60, 227)
	
	// 6 - Continuation of the Sonic CD bar
	SpriteFrame(16, 81, 160, 16, 52, 227)
	
	// 7 - Set of three lines
	SpriteFrame(100, 32, 24, 48, 182, 124)
	
	// 8-19 - Individual letters spelling out "Special Stage"
	SpriteFrame(0, 0, 16, 55, 207, 124)		// #8  - S
	SpriteFrame(17, 32, 10, 23, 176, 180)	// #9  - P
	SpriteFrame(28, 32, 9, 23, 187, 180)	// #10 - E
	SpriteFrame(38, 32, 8, 23, 197, 180)	// #11 - C
	SpriteFrame(47, 32, 6, 23, 206, 180)	// #12 - I
	SpriteFrame(54, 32, 11, 23, 212, 180)	// #13 - A
	SpriteFrame(66, 32, 9, 23, 176, 203)	// #14 - L
	
	SpriteFrame(0, 56, 16, 55, 207, 124)	// #15 - S
	SpriteFrame(17, 56, 10, 23, 186, 203)	// #16 - T
	SpriteFrame(28, 56, 11, 23, 212, 180)	// #17 - A
	SpriteFrame(40, 56, 10, 23, 197, 203)	// #18 - G
	SpriteFrame(51, 56, 9, 23, 187, 180)	// #19 - E

	SpriteFrame(-128,-128,256,0,256,256) //20	

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
